package com.yantong.service.impl;

import com.yantong.exception.BDException;
import com.yantong.mapper.MemberMapper;
import com.yantong.mapper.StoreFreechargeMapper;
import com.yantong.pojo.Member;
import com.yantong.pojo.StoreFreecharge;
import com.yantong.service.StoreFreeChargeService;
import com.yantong.utils.PageUtils;
import com.yantong.utils.ValidateUtil;
import org.apache.ibatis.session.RowBounds;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Map;

@Service
public class StoreFreeChargeServiceImpl implements StoreFreeChargeService {

    private static final Logger logger = LoggerFactory.getLogger(StoreFreeChargeServiceImpl.class);

    @Autowired
    private StoreFreechargeMapper storeFreechargeMapper;
    @Autowired
    private MemberMapper memberMapper;

    @Override
    public PageUtils<StoreFreecharge> list(Map map) {
        PageUtils<StoreFreecharge> storeFreechargePageUtils = new PageUtils<>();
        storeFreechargePageUtils.setRows(storeFreechargeMapper.pageListBycondition(map, new RowBounds(
                Integer.parseInt(map.get("offset").toString()), Integer.parseInt(map.get("limit").toString()))));
        int total = storeFreechargeMapper.queryTotal(map);
        storeFreechargePageUtils.setTotal(total);
        return storeFreechargePageUtils;
    }

    @Override
    public int addStoreFreeCharge(StoreFreecharge storeFreecharge) {
        if (null == storeFreecharge) {
            throw new BDException("信息异常，请重试！");
        }
        if (ValidateUtil.isEmptyString(storeFreecharge.getTitle())) {
            throw new BDException("请输入名称！");
        }
        if (null == storeFreecharge.getCount() || storeFreecharge.getCount() == 0) {
            throw new BDException("请输入份数！");
        }
//        if (null == storeFreecharge.getPrice() || storeFreecharge.getPrice().compareTo(BigDecimal.ZERO) == 0) {
//            throw new BDException("请输入原价！");
//        }
        if(null == storeFreecharge.getTeamId()){
            throw new BDException("门店信息异常，请重试！");
        }

        Member shopMember =memberMapper.selectByPrimaryKey(storeFreecharge.getTeamId());
        if(null == shopMember){
            throw new BDException("门店信息异常，请重试！");
        }
        //设置店铺的免单标志
        if(shopMember.getIsFreecharge() == null || shopMember.getIsFreecharge() == 0){
            shopMember.setIsFreecharge(1);
            memberMapper.updateByPrimaryKeySelective(shopMember);
        }

        return storeFreechargeMapper.insertSelective(storeFreecharge);
    }

    @Override
    public int editStoreFreeCharge(StoreFreecharge storeFreecharge) {
        if (null == storeFreecharge) {
            throw new BDException("信息异常，请重试！");
        }

        if (storeFreecharge.getId() == null) {
            throw new BDException("信息异常，请重试！");
        }

        if (ValidateUtil.isEmptyString(storeFreecharge.getTitle())) {
            throw new BDException("请输入名称！");
        }
        if (null == storeFreecharge.getCount() || storeFreecharge.getCount() == 0) {
            throw new BDException("请输入份数！");
        }
        if (null == storeFreecharge.getPrice() || storeFreecharge.getPrice().compareTo(BigDecimal.ZERO) == 0) {
            throw new BDException("请输入原价！");
        }

        Member shopMember =memberMapper.selectByPrimaryKey(storeFreecharge.getTeamId());
        if(null == shopMember){
            throw new BDException("门店信息异常，请重试！");
        }
        //设置店铺的免单标志
        if(shopMember.getIsFreecharge() == null || shopMember.getIsFreecharge() == 0){
            shopMember.setIsFreecharge(1);
            memberMapper.updateByPrimaryKeySelective(shopMember);
        }

        return storeFreechargeMapper.updateByPrimaryKeySelective(storeFreecharge);
    }

    @Override
    public int deleteStoreFreeCharge(StoreFreecharge storeFreecharge) {
        if (null == storeFreecharge) {
            throw new BDException("信息异常，请重试！");
        }

        if (storeFreecharge.getId() == null) {
            throw new BDException("信息异常，请重试！");
        }

        return storeFreechargeMapper.deleteByPrimaryKey(storeFreecharge.getId());
    }

    @Override
    public StoreFreecharge selectByPrimaryKey(Long id) {
        return storeFreechargeMapper.selectByPrimaryKey(id);
    }
}
